package nl.weeaboo.sh.editor;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JToggleButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;

import nl.weeaboo.sh.editor.area.ChangeListener;

@SuppressWarnings("serial")
public class HitBoxModeSelector extends JPanel {

	public enum Mode {
		IMAGE("Image"),
		HIT_RECT("Hit Rect"),
		SPLASH_RECT("Splash Rect"),
		DAMAGE_RECT("Damage Rect"),
		COL_CIRCLE("Collision Circle"),
		ANCHOR("Anchor");
		
		private String label;
		
		Mode(String l) { label = l; }
		
		public String toString() { return label; }
	}
	
	private List<ChangeListener> listeners;
	private Mode mode = Mode.ANCHOR;
	private int rectIndex;
	
	private JToggleButton anchorButton;
	private JToggleButton imageButton;
	private JToggleButton hitRectButton;
	private JToggleButton colCircleButton;
	private JSpinner colCircleSpinner;
	private JToggleButton damageRectButton;
	private JSpinner damageRectSpinner;
	private JToggleButton splashRectButton;
	private JSpinner splashRectSpinner;
	
	public HitBoxModeSelector() {
		listeners = new LinkedList<ChangeListener>();		

		anchorButton = new JToggleButton(Mode.ANCHOR.toString());
		anchorButton.setSelected(true);
		anchorButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setMode(Mode.ANCHOR);
			}
		});
		
		imageButton = new JToggleButton(Mode.IMAGE.toString());
		imageButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setMode(Mode.IMAGE);
			}
		});
		
		hitRectButton = new JToggleButton(Mode.HIT_RECT.toString());
		hitRectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setMode(Mode.HIT_RECT);
			}
		});
		
		damageRectButton = new JToggleButton(Mode.DAMAGE_RECT.toString());
		damageRectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mode = null;
				rectIndex = (Integer)damageRectSpinner.getValue();
				damageRectButton.setSelected(true);
				setMode(Mode.DAMAGE_RECT);
			}
		});
		
		damageRectSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
		damageRectSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				mode = null;
				rectIndex = (Integer)damageRectSpinner.getValue();
				damageRectButton.setSelected(true);
				setMode(Mode.DAMAGE_RECT);
			}
		});
		
		splashRectButton = new JToggleButton(Mode.SPLASH_RECT.toString());
		splashRectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mode = null;
				rectIndex = (Integer)splashRectSpinner.getValue();
				splashRectButton.setSelected(true);
				setMode(Mode.SPLASH_RECT);
			}
		});
		
		splashRectSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
		splashRectSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				mode = null;
				rectIndex = (Integer)splashRectSpinner.getValue();
				splashRectButton.setSelected(true);
				setMode(Mode.SPLASH_RECT);
			}
		});
		
		colCircleButton = new JToggleButton(Mode.COL_CIRCLE.toString());
		colCircleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mode = null;
				rectIndex = (Integer)colCircleSpinner.getValue();
				colCircleButton.setSelected(true);
				setMode(Mode.COL_CIRCLE);
			}
		});
		
		colCircleSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 99, 1));
		colCircleSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				mode = null;
				rectIndex = (Integer)colCircleSpinner.getValue();
				colCircleButton.setSelected(true);
				setMode(Mode.COL_CIRCLE);
			}
		});
		
		ButtonGroup group = new ButtonGroup();
		group.add(anchorButton);
		group.add(imageButton);
		group.add(hitRectButton);
		group.add(damageRectButton);
		group.add(splashRectButton);
		group.add(colCircleButton);
		
		setBorder(new EmptyBorder(0, 0, 5, 5));
		setPreferredSize(new Dimension(200, 100));
		setLayout(new GridLayout(-1, 2, 0, 0));
		add(anchorButton);
		add(Box.createGlue());
		add(imageButton);
		add(hitRectButton);
		add(damageRectButton);
		add(damageRectSpinner);
		add(splashRectButton);
		add(splashRectSpinner);
		add(colCircleButton);
		add(colCircleSpinner);
	}
	
	//Functions
	public void addChangeListener(ChangeListener l) {
		listeners.add(l);
	}
	public void removeChangeListener(ChangeListener l) {
		listeners.remove(l);
	}
	
	protected void fireModeChanged(final Mode mode) {
		ChangeListener ls[] = listeners.toArray(new ChangeListener[listeners.size()]);
		for (ChangeListener l : ls) {
			l.onChanged(mode);
		}
	}
	
	//Getters
	public int getRectIndex() { return rectIndex; }
	
	//Setters
	public void setMode(Mode m) {
		if (mode != m) {
			mode = m;
			
			fireModeChanged(m);
		}
	}
	
}
